﻿using Vestras.StarCraft2.Luna.Parser;
using Vestras.StarCraft2.Luna.Parser.Ast;

namespace Vestras.StarCraft2.Luna.Validation {
    public sealed class ContinueStatementTypeValidator : ITypeValidator {
        public System.Type[] Type {
            get {
                return new[] { typeof(ContinueStatement) };
            }
        }

        public bool Validate(object value) {
            if (value != null && value is ContinueStatement) {
                ErrorSink.UpdateChangeCollection();
                ContinueStatement continueStatement = value as ContinueStatement;
                WhileStatement parent = continueStatement.GetLogicalParentOfType<WhileStatement>();
                if (parent == null) {
                    ErrorSink.AddError(ErrorSink.Error.Create("A continue statement must be the logical child of a while statement.", continueStatement));
                    return false;
                }

                return true;
            }

            return false;
        }
    }
}
